package cn.zhuizikejithreeig.com.domeapplication;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

public class MainActivity extends AppCompatActivity {

    private ProgressView mProgressView;
    private TextView mTextView;
    private Handler handler = new Handler();
    private Handler handlerTwo = new Handler();
    private int minus = 10000;
    private ProgressbackView mProgressBackView;
    private int add = 0;
    private boolean iscontiun = true;
    private ChengeBackView mChengeBackView;
    private int newAdd = 0;
    private ChengeSecondBackView mChengeSecondBackView;
    private ImageView mOneImageView;
    private ImageView mTwoImageView;
    private ImageView mThreeImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mProgressView = (ProgressView) findViewById(R.id.progressview);
        mProgressBackView = (ProgressbackView) findViewById(R.id.progressbackview);
        mChengeBackView=(ChengeBackView) findViewById(R.id.chengebackview);
        mChengeSecondBackView=(ChengeSecondBackView) findViewById(R.id.chengesecondbackview);
        mOneImageView=(ImageView) findViewById(R.id.one);
        mTwoImageView=(ImageView) findViewById(R.id.two);
        mThreeImageView=(ImageView) findViewById(R.id.three);



        mChengeBackView.setPaintColor("#3F51B5");
        mChengeSecondBackView.setPaintColor("#3F51B5");
        mProgressBackView.setPaintColor("#ffffff");
        mTextView = (TextView) findViewById(R.id.text);
        mTextView.setText(timeChenge(minus));


    }

    public void button(View view) {
        if (iscontiun) {
            mTextView.setText(timeChenge(minus));
            iscontiun = false;
            mProgressView.setPaintColor("#ff0000");
            mProgressView.startDownTime(minus, new OnFinishListener() {
                @Override
                public void onFinish() {
                    minus = 10000;
                }
            });
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    minus = minus - 1000;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (minus <= 0) {
                                iscontiun = true;
                                mTextView.setText("倒计时结束");
                                return;
                            }
                            mTextView.setText(timeChenge(minus));
                        }
                    });
                    if (minus <= 0) {
                        return;
                    }
                    iscontiun = false;
                    handler.postDelayed(this, 1000);
                }
            }, 1000);
        }

    }

    // 位数不足补零
    private String fill_zero_prefix(int num) {
        return num < 10 ? "0" + num : num + "";
    }

    private String timeChenge(int time) {
        // 秒数
        int second = (time / 1000);
        //小时数
        int hr = second / 3600;
        //分钟位
        String min = fill_zero_prefix((int) (second - hr * 3600) / 60);
        // 秒位
        String sec = fill_zero_prefix((second - hr * 3600 - Integer.parseInt(min) * 60));
        return min + ":" + sec;
    }
    private String address = "ws://192.168.0.200:1994";
    private URI uri;
    private WebSocketClient mWebSocketClient;
    public void buttontwo(View view) {
        Animation animationOne= AnimationUtils.loadAnimation(MainActivity.this,R.anim.player_double_click_animation);
        final Animation animationTwo= AnimationUtils.loadAnimation(MainActivity.this,R.anim.player_double_click_animation);
        mOneImageView.setVisibility(View.VISIBLE);
        mOneImageView.startAnimation(animationOne);
        animationOne.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(final Animation animation) {

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mTwoImageView.setVisibility(View.VISIBLE);
                                mTwoImageView.startAnimation(animationTwo)
                                ;
                            }
                        });

                    }
                }, 2000);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mOneImageView.startAnimation(animation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


//        try {
//            uri = new URI(address);
//        } catch (URISyntaxException e) {
//            e.printStackTrace();
//        }
//        if (null == mWebSocketClient) {
//            mWebSocketClient = new WebSocketClient(uri) {
//                @Override
//                public void onOpen(ServerHandshake serverHandshake) {
//                    System.out.println("通道已经打开");
//                    mWebSocketClient.send("123");
//                }
//                @Override
//                public void onMessage(String s) {
//                    System.out.println("发送消息");
//                }
//                @Override
//                public void onClose(int i, String s, boolean b) {
//                    System.out.println("通道关闭");
//                }
//                @Override
//                public void onError(Exception e) {
//                    System.out.println("通道出错");
//                }
//            };
//            mWebSocketClient.connect();
//        }


//        handler.post(new Runnable() {
//            @Override
//            public void run() {
//                add +=1;
//
//                if (add==100){
//                    add = 0;
//                    handler.removeCallbacks(this);
//                }
//
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        mChengeBackView.setAlpha(100-add);
//                    }
//                });
//                mChengeBackView.addRedio(add);
//                handler.postDelayed(this, 20);
//            }
//        });
//
    }
}
